package dr.joketalker.account;

import dr.joketalker.dao.FactoryDAO;
import dr.joketalker.dao.UserDAO;
import dr.joketalker.entity.User;
public class AccountManager {

	
	static AccountManager stAccountManager = null;
	
	protected User userLogged = null;
	UserDAO userDao = FactoryDAO.getUserDao();
	
	private AccountManager(){
	}
	
	public static AccountManager getInstance(){
		if(stAccountManager == null){
			stAccountManager = new AccountManager();
		}
		return stAccountManager;
	}
	
	public boolean login(String name,String password){

		User cUser  = userDao.readByName(name);
		
		if(cUser!=null && cUser.getPassword().equals(password)){
			userLogged = cUser;
			return true;
		}			
		return false;
	}
	
	public boolean loginAsGuest(){
		User cUser  = userDao.readByName("Guest");
		
		if(cUser!=null){
			userLogged = cUser;
			return true;
		}			
		return false;
	}
	
	
	public User getLoggedUser(){
		if(userLogged == null){
			//gast
			return userDao.read(1);		
		}
		
		return userLogged;
	}
}
